package com.wankuma.hcm.midlet.metwit;

import javax.microedition.rms.RecordStore;

public class Config {
	private static Config instance;

	public synchronized static Config getInstance() {
		if (instance == null) {
			instance = new Config();
		}
		return instance;
	}

	private String user;
	private String password;
	private int pageCapacity;
	private int timeLineCapacity;
	private int timerInterval;

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getPageCapacity() {
		return pageCapacity;
	}

	public void setPageCapacity(int pageCapacity) {
		if (pageCapacity < 5)
			pageCapacity = 5;
		this.pageCapacity = pageCapacity;
	}

	public int getTimeLineCapacity() {
		return timeLineCapacity;
	}

	public void setTimeLineCapacity(int timeLineCapacity) {
		if (timeLineCapacity < 20)
			timeLineCapacity = 20;
		this.timeLineCapacity = timeLineCapacity;
	}

	public int getTimerInterval() {
		return timerInterval;
	}

	public void setTimerInterval(int timerInterval) {
		if (timerInterval < 2)
			timerInterval = 2;
		this.timerInterval = timerInterval;
	}

	private Config() {
		loadConfig();
	}

	public void loadConfig() {
		RecordStore rs = null;
		try {
			rs = RecordStore.openRecordStore("METwit", false);
			if (rs != null) {
				int len = rs.getNumRecords();
				if (len > 0)
					this.user = new String(rs.getRecord(1));
				else
					this.user = "";
				if (len > 1)
					this.password = new String(rs.getRecord(2));
				else
					this.user = "";
				if (len > 2)
					this.pageCapacity = BinaryUtil.byteToInt(rs.getRecord(3));
				else
					this.pageCapacity = 10;
				if (len > 3)
					this.timeLineCapacity = BinaryUtil.byteToInt(rs
							.getRecord(4));
				else
					this.timeLineCapacity = 50;
				if (len > 4)
					this.timerInterval = BinaryUtil.byteToInt(rs
							.getRecord(5));
				else
					this.timerInterval = 2;
			}
		} catch (Exception ex) {
			this.user = "";
			this.password = "";
			this.pageCapacity = 10;
			this.timeLineCapacity = 50;
			this.timerInterval = 2;

			Alerter.alert("初期設定を行ってください。");
		} finally {
			try {
				if (rs != null)
					rs.closeRecordStore();
			} catch (Exception e) {
				Alerter.alert(e.toString());
			}
		}
	}

	public void saveConfig() {
		RecordStore rs = null;
		try {
			try {
				RecordStore.deleteRecordStore("METwit");
			} catch (Exception ex) {
			}
			rs = RecordStore.openRecordStore("METwit", true);
			byte[] bytesUser = user.getBytes();
			byte[] bytesPassword = password.getBytes();
			byte[] bytesPageCapacity = BinaryUtil.intToByte(pageCapacity);
			byte[] bytesTimeLineCapacity = BinaryUtil
					.intToByte(timeLineCapacity);
			byte[] bytesTimerInterval = BinaryUtil.intToByte(timerInterval);
			rs.addRecord(bytesUser, 0, bytesUser.length);
			rs.addRecord(bytesPassword, 0, bytesPassword.length);
			rs.addRecord(bytesPageCapacity, 0, bytesPageCapacity.length);
			rs
					.addRecord(bytesTimeLineCapacity, 0,
							bytesTimeLineCapacity.length);
			rs.addRecord(bytesTimerInterval, 0, bytesTimerInterval.length);
		} catch (Exception ex) {
			Alerter.alert(ex.toString());
		} finally {
			try {
				if (rs != null)
					rs.closeRecordStore();
			} catch (Exception e) {
				Alerter.alert(e.toString());
			}
		}
	}
}
